<?php

defined('BASEPATH') or die('forbidden');

class Language_Controller Extends Controller {

    var $dir = '';

    function _init() {
        check_level(50, segment(0), segment(1));
        $this->dir = dirname(BASEPATH) . '/api/application/' . 'languages/';
    }

    function index() {
        $this->view->display("general/language/index");
    }

    function flexigrid() {
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
        $rows = array();
        $files = scandir($this->dir);
        $fileinfo = array();
        foreach ($files as $file) {
            if ($file == '.' || $file == '..')
                continue;
            $absfile = $this->dir . '/' . $file;
            $fileinfo[] = array('id' => $file, 'name' => $file, 'size' => filesize($absfile), 'atime' => fileatime($absfile), 'mtime' => filemtime($absfile));
        }
        $fileinfo = array_sort($fileinfo, $sortname, "SORT_" . strtoupper($sortorder));
        $rows = array();
        foreach ($fileinfo as $m => $n) {
            $n['size'] = format_filesize($n['size']);
            $n['mtime'] = get_datetime($n['mtime']);
            $n['atime'] = get_datetime($n['atime']);
            $rows[] = array('id' => $n['id'], 'cell' => array_values($n));
        }
        $result = array(
            'page' => $page,
            'total' => count($rows),
            'rows' => $rows
        );
        echo json_encode($result);
    }

    function add() {
        $id = segment(3);
        if ($id === 0)
            error(translate('C_INVALID_PARAM_ID', $id));
        $file = $this->dir . $id;
        if (!is_file($file))
            json_output(0, translate('GENERAL_LANGUAGE_NOFOUND', $id));
        
        if (is_submit()) {
            $dest = $this->dir . str_replace(".", "_new.", $id);
            if (!is_file($dest)) {
                copy($file, $dest);
                json_output(1, '');
            } else {
                json_output(0, translate('GENERAL_LANGUAGE_ALEXIST'));
            }
        }
    }

    function edit() {
        $id = segment(3);
        if ($id === 0)
            error(translate('C_INVALID_PARAM_ID', $id));
        $file = $this->dir . $id;
        if (!is_file($file))
            error(translate('GENERAL_LANGUAGE_NOFOUND', $id));
        include $file;
        $row = $lang;
        
        if (is_submit()) {
            $rowdata = array();
            foreach ($this->input->post("row") as $m => $n) {
                $rowdata[$n['name']] = $n['desc'];
            }
            ksort($rowdata);
            ob_start();
            echo "<?php \ndefined('BASEPATH') or die('forbidden');\n\$lang = ";
            echo array2val($rowdata);
            echo ";";
            $content = ob_get_contents();
            ob_clean();
            $rowdata['id'] = $id;
            $newid = $this->input->post("id");
            //如果修改了文件名,则删除原来的再创建新的
            if ($id != $newid) {
                $newfile = $this->dir . $newid;
                if (is_file($newfile)) {
                    error(translate('GENERAL_LANGUAGE_EXIST'));
                } else {
                    @unlink($file);
                    $file = $newfile;
                    $rowdata['newid'] = $newid;
                }
            }
            @file_put_contents($file, $content);
            cp_tips("success", $id);
        }
        $this->view->display("general/language/edit", array('row' => $row, 'id' => $id));
    }

    function del() {
        $c = 0;
        $r = '';
        $id = segment(3, 0);
        if ($id === 0)
            json_output($c, translate('C_INVALID_PARAM_ID', $id));
        $file = $this->dir . $id;
        @unlink($file);
        $c = 1;
        $r = '';
        json_output($c, $r);
    }

    function multi() {
        $type = $this->input->post("type");
        $ids = $this->input->post("ids");
        $c = 0;
        $r = '';
        if (count(explode(',', $ids)) == 0) {
            $r = translate('C_INVALID_PARAMS');
        } else {
            $allids = $ids = explode(',', $ids);
            if ($type == 'delete') {
                foreach ($ids as $m => $n) {
                    $file = $this->dir . $n;
                    @unlink($file);
                }
                $id = implode(',', $ids);
                $r = '';
                $c = 1;
            } else {
                $r = translate('C_INVALID_PARAMS');
            }
        }
        json_output($c, $r);
    }

}